package com.fbli.springdatajpa.service.impl;

import com.fbli.springdatajpa.pojo.entity.JpaUser;
import com.fbli.springdatajpa.repository.JpaUserRepository;
import com.fbli.springdatajpa.service.JpaUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class JpaUserServiceImpl implements JpaUserService {
    @Resource
    private JpaUserRepository jpaUserRepository;

    @Override
    public JpaUser insertUser(JpaUser user) {
        return jpaUserRepository.save(user);
    }

    @Override
    public void deleteUser(Long id) {
        jpaUserRepository.deleteById(id);
    }

    @Override
    public JpaUser updateUser(JpaUser user) {
        return jpaUserRepository.save(user);
    }

    @Override
    public List<JpaUser> findAllUser() {
        return jpaUserRepository.findAll();
    }

    @Override
    public JpaUser findUserById(Long id) {
        return jpaUserRepository.findById(id).orElse(null);
    }

    @Override
    public String login(HttpSession session, JpaUser jpaUser) {
        JpaUser user = jpaUserRepository.findByName(jpaUser.getName());
        if (user !=null){
            session.setAttribute("jpaUser",jpaUser);
            return "ok";
        }else {
            return "error";
        }
    }
}

